import pandas as pd
import matplotlib.pyplot as plt

from sklearn.model_selection import train_test_split
from sklearn.preprocessing import MinMaxScaler
from sklearn.linear_model import LogisticRegression

#1.加载数据集
dataset = pd.read_csv('../data/train.csv')

#测试图像
# digit = dataset.iloc[10,1:].values
# plt.imshow(digit.reshape(28, 28),cmap='gray')
# plt.show()

#2.划分数据集
X = dataset.drop('label',axis=1)
Y = dataset['label']
X_train, X_test, Y_train, Y_test = train_test_split(X, Y, test_size=0.3, random_state=42)
#print(X_train.shape, X_test.shape, Y_train.shape, Y_test.shape)

#3.特征工程 归一化
scaler = MinMaxScaler()
X_train = scaler.fit_transform(X_train)
X_test = scaler.transform(X_test)
#print(X_train.shape, X_test.shape, Y_train.shape, Y_test.shape)

#4.定义模型和训练
model = LogisticRegression(max_iter=500)
model.fit(X_train, Y_train)

#5.模型评估
score = model.score(X_test, Y_test)
print(score)

#6.测试 预测某个新图像表示的数字
digit = X_test[123,:].reshape(1,-1)
print(model.predict(digit))
print(Y_test.iloc[123])
print(model.predict_proba(digit))

#画出图形
plt.imshow(digit.reshape(28,28),cmap='gray')
plt.show()

